package com.neusoft.jxc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neusoft.jxc.bean.Pager;
import com.neusoft.jxc.bean.Result;
import com.neusoft.jxc.bean.SysUser;
import com.neusoft.jxc.service.SysUserService;
import com.neusoft.jxc.util.PasswordUtils;

/**
 * 
 * @author chens
 *
 */
@Controller
@RequestMapping("sysuser")
public class SysUserController {

	@Autowired
	private SysUserService userService = null;

	/**
	 * 当页面传递的参数与方法的参数名称不一样时，可以使用@RequestParam进行参数名称设置，
	 * 
	 * @RequestParam 标签还可以在没有对应参数时指定一个默认值
	 * 
	 * 
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Pager<SysUser> list(@RequestParam(defaultValue = "1", value = "page") Integer page,
			@RequestParam(defaultValue = "10", value = "rows") Integer size) {
		Pager<SysUser> pager = new Pager<SysUser>(page, size);

		List<SysUser> list = userService.findList(pager);
		int count = userService.findTotal(pager);
		pager.setRows(list);
		pager.setTotal(count);

		return pager;
	}

	@RequestMapping("add")
	@ResponseBody
	public Result add(SysUser user) {
		Result result = new Result(Boolean.FALSE, "新增失败");
		
		boolean ret = userService.add(user);
		if (ret) {
			result = new Result(Boolean.TRUE, "新增成功");
		}
		return result;
	}

	@RequestMapping("edit")
	@ResponseBody
	public Result edit(SysUser user) {
		Result result = new Result(Boolean.FALSE, "修改失败");
		boolean ret = userService.edit(user);
		if (ret) {
			result = new Result(Boolean.TRUE, "修改成功");
		}
		return result;
	}

	@RequestMapping("delete")
	@ResponseBody
	public Result delete(SysUser user) {
		Result result = new Result(Boolean.FALSE, "删除失败");
		boolean ret = userService.deleteById(user);
		if (ret) {
			result = new Result(Boolean.TRUE, "删除成功");
		}
		return result;
	}
}
